package com.funmi.lizip.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.funmi.lizip.common.base.BeanToArrayUtil;
import com.funmi.lizip.common.base.DeleteRequest;
import com.funmi.lizip.common.base.R;
import com.funmi.lizip.common.base.ResultCode;
import com.funmi.lizip.common.exception.ApplicationException;
import com.funmi.lizip.entity.EcPostDisasterAssessment;
import com.funmi.lizip.entity.dto.ecPostDisasterAssessment.EcPostDisasterAssessmentAddRequest;
import com.funmi.lizip.entity.dto.ecPostDisasterAssessment.EcPostDisasterAssessmentQueryRequest;
import com.funmi.lizip.entity.vo.EcPostDisasterAssessmentVo;
import com.funmi.lizip.service.EcPostDisasterAssessmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wangdc
 * @since 2022-11-02
 */
@Api(tags = "灾后评估")
@RestController
@RequestMapping("/postDisasterAssessment")
public class EcPostDisasterAssessmentController {

    @Autowired
    private EcPostDisasterAssessmentService ecPostDisasterAssessmentService;

    @ApiOperation(value = "新增")
    @PostMapping("/add")
    public R addEcPostDisasterAssessment(@RequestBody EcPostDisasterAssessmentAddRequest request){
        if (request == null) {
            throw new ApplicationException(ResultCode.PARAMS_ERROR);
        }
        EcPostDisasterAssessment ecPostDisasterAssessment = new EcPostDisasterAssessment();
        BeanUtils.copyProperties(request, ecPostDisasterAssessment);
        boolean result = ecPostDisasterAssessmentService.save(ecPostDisasterAssessment);
        if (result) {
            return R.success().message("新增成功");
        } else {
            throw new ApplicationException("新增失败");
        }
    }

    @ApiOperation(value = "编辑")
    @PutMapping("/edit")
    public R updateEcPostDisasterAssessment(@RequestBody EcPostDisasterAssessmentAddRequest request){
        if (request == null) {
            throw new ApplicationException(ResultCode.PARAMS_ERROR);
        }
        EcPostDisasterAssessment ecPostDisasterAssessment = new EcPostDisasterAssessment();
        BeanUtils.copyProperties(request, ecPostDisasterAssessment);
        boolean result = ecPostDisasterAssessmentService.updateById(ecPostDisasterAssessment);
        if (result) {
            return R.success().message("编辑成功");
        } else {
            throw new ApplicationException("编辑失败");
        }
    }

    @ApiOperation(value = "获取灾后评估详情")
    @ApiImplicitParam(name = "id",value = "灾后评估详情ID",dataTypeClass = String.class, required = true)
    @GetMapping("/get")
    public R getDetail(int id) throws ParseException {
        EcPostDisasterAssessment ecPostDisasterAssessment = ecPostDisasterAssessmentService.getById(id);

        EcPostDisasterAssessmentVo ecPostDisasterAssessmentVo = new EcPostDisasterAssessmentVo();
        BeanUtils.copyProperties(ecPostDisasterAssessment, ecPostDisasterAssessmentVo);


//        Map map = new HashMap<>();
//        SimpleDateFormat sf=  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//
//        for (Object key: ecPostDisasterAssessmentMap.keySet()) {
//
//            if (Objects.equals(key, "fireTime") || Objects.equals(key, "createAt")) {
//                String formatTimeStr =  sf.format(ecPostDisasterAssessmentMap.get(key));
//                map.put(key, formatTimeStr);
//            } else {
//                map.put(key, ecPostDisasterAssessmentMap.get(key));
//            }
//
//        }
        return R.success().data(ecPostDisasterAssessmentVo);
    }

    @ApiOperation(value = "删除")
    @DeleteMapping("/delete")
    public R deleteEcPostDisasterAssessment(@RequestBody DeleteRequest request){
        if (request == null || request.getId() <= 0) {
            throw new ApplicationException(ResultCode.FILE_DELETE_ERROR);
        }
        boolean result = ecPostDisasterAssessmentService.delete(request.getId());
        if (result) {
            return R.success().message("删除成功");
        } else {
            throw new ApplicationException("删除失败");
        }
    }

    @ApiOperation(value = "根据输入进行检索")
    @GetMapping("/queryList/page")
    public R queryList(EcPostDisasterAssessmentQueryRequest request){
        long current = request.getCurrent();
        long size = request.getPageSize();

        QueryWrapper<EcPostDisasterAssessment> queryWrapper = new QueryWrapper<>();
        String keywords =  request.getKeywords();
        String startTime = request.getCreateAtStart();
        String endTime = request.getCreateAtEnd();

        queryWrapper.eq("is_delete", false);
        if (keywords != null) {
            queryWrapper.and(qw -> {
                qw.nested(w -> w.like("title", keywords).or().like("content", keywords).or().like("main_tree", keywords));
            });
        }
        if (startTime != null) {
            queryWrapper.ge("create_at", startTime);
        }
        if (endTime != null) {
            queryWrapper.le("create_at", endTime);
        }
        Page<EcPostDisasterAssessment> ecPostDisasterAssessmentPage = ecPostDisasterAssessmentService.page(new Page<>(current, size), queryWrapper);

        Page<EcPostDisasterAssessmentVo> ecPostDisasterAssessmentVoPage = new PageDTO(ecPostDisasterAssessmentPage.getCurrent(), ecPostDisasterAssessmentPage.getSize(), ecPostDisasterAssessmentPage.getTotal());
        List<EcPostDisasterAssessmentVo> ecPostDisasterAssessmentList = ecPostDisasterAssessmentPage.getRecords().stream().map(ecPostDisasterAssessment -> {
            EcPostDisasterAssessmentVo ecPostDisasterAssessmentVo = new EcPostDisasterAssessmentVo();
            BeanUtils.copyProperties(ecPostDisasterAssessment, ecPostDisasterAssessmentVo);
            return ecPostDisasterAssessmentVo;
        }).collect(Collectors.toList());

        ecPostDisasterAssessmentVoPage.setRecords(ecPostDisasterAssessmentList);
        return R.success().data(ecPostDisasterAssessmentVoPage);
    }
}

